package onion.molingo2o.activity.service;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;

import com.easemob.chatuidemo.ui.BaseActivity;

import java.io.File;

import onion.molingo2o.R;
import onion.molingo2o.activity.ablumimage.AlbumActivity;
import onion.molingo2o.activity.ablumimage.ImagePagerActivity;
import onion.molingo2o.utils.FileUtils;
import onion.molingo2o.utils.ImageUtils;
import onion.molingo2o.utils.SelectImgUtil;
import onion.molingo2o.utils.imageutil.BimpRotateUtil;
import onion.molingo2o.utils.imageutil.BitmapHelper;

/**
 * 宣传介绍页
 * Created by Administrator on 2015/12/20.
 */
public class ServerIntroduceActivity extends BaseActivity {
    SelectImgUtil selectImgUtil;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_serverintroudce);
        initTitle(null, "店铺介绍", R.drawable.tx_actionbar_speak_bg);
        selectImgUtil=new SelectImgUtil(this);
    }




    @Override
    protected void onDestroy() {
        BitmapHelper.cleanBitmap();
        super.onDestroy();

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if (keyCode == KeyEvent.KEYCODE_BACK) {

        }

        return super.onKeyDown(keyCode, event);
    }





    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == AlbumActivity.resultCode_to_NewActivity) {
            if (data != null) {
              selectImgUtil.  refreshGridView();
            }
        } else if (resultCode == ImagePagerActivity.resultCode_to_newAcitivity) {
            selectImgUtil. refreshGridView();
        }
        if (requestCode == SelectImgUtil.requestCode_to_Photo) {
            Log.i("onion", "data" + data);
            String photoName=selectImgUtil.photoName;
            if (data != null) { //可能尚未指定intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
                //返回有缩略图
                if (data.hasExtra("data")) {

                    Bitmap thumbnail = data.getParcelableExtra("data");
                    //Bitmap thumbnail= null;
                    try {
                        int degree = BimpRotateUtil.getBitmapDegree(photoName);
                        thumbnail = BimpRotateUtil.rotateBitmapByDegree(thumbnail, degree);

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    //得到bitmap后的操作
                    FileUtils.saveBitmap(thumbnail, photoName);
                    // FileUtils.saveMyBitmap(photoName,thumbnail );
                    File f = new File(photoName);
                    if (f.exists()) {
                        ImageUtils.rotate(BitmapFactory.decodeFile(photoName), ImageUtils.readPictureDegree(photoName));
                        BitmapHelper.imageListStorage.add(photoName);
                        selectImgUtil.refreshGridView();
                    }
                }
            } else {
                //由于指定了目标uri，存储在目标uri，intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
                // 通过目标uri，找到图片
                // 对图片的缩放处理
                // 操作
                ImageUtils.rotate(BitmapFactory.decodeFile(photoName), ImageUtils.readPictureDegree(photoName));
                BitmapHelper.imageListStorage.add(photoName);
                selectImgUtil.refreshGridView();
            }

        }
        if (requestCode == SelectImgUtil.requestCode_to_imagePager) {
            selectImgUtil.refreshGridView();
        }
    }
}
